package vn.com.epi.re.tools;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import vn.com.epi.re.data.Pair;

public class StringUtils {
    public static final String EMPTY_STRING = "";
    
    public static boolean validInteger(String integer) {
        try {
            Integer.parseInt(integer);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
    
    public static String arrayToString(String[] array) {
        if (array.length == 0)
            return null;
        else {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < array.length; i++)
                sb.append(array[i]).append(",");
            deleteLastChar(sb);
            return sb.toString();
        }
    }

    public static int getNumberOfItems(String string) {
        try {
            return string.split(",").length;
        } catch (Exception e) {
            return 0;
        }
    }
    
    public static void insertCommaChar(StringBuilder sb) {
        if (sb.length() > 0)
            sb.append(",");
    }
    
    public static void deleteLastChar(StringBuilder sb) {
        if (sb.length() > 0)
            if (sb.charAt(sb.length() - 1) == ',')
                sb.deleteCharAt(sb.length() - 1);
    }       
    
    public static String getSubSet(List<Pair> set, int howMany) {
        if (set == null)
            return EMPTY_STRING;
        else {
            StringBuilder sb = new StringBuilder();
            int index = 0;
            Collections.sort(set);
            for (Pair item: set) {
                sb.append(item.getId() + ":" + item.getScore()).append(",");                
                index++;
                if (index == howMany)
                    break;
            }
            deleteLastChar(sb);
            return sb.toString();
        }
    }
    
    public static String mergeRecommendations(String oldR, String newR, int howMany) {
        try {
            Map<Integer, Double> map = Maps.newHashMap();
            if (!Strings.isNullOrEmpty(oldR)) {
                for (String s : oldR.split(",")) {
                    String[] chunks = s.split(":");
                    int articleId = Integer.parseInt(chunks[0]);
                    double score = Double.parseDouble(chunks[1]);
                    if (map.containsKey(articleId)) {
                        double oldScore = map.get(articleId);
                        map.put(articleId, Math.max(oldScore, score));
                    } else
                        map.put(articleId, score);
                }
            }
            if (!Strings.isNullOrEmpty(newR)) {
                for (String s : newR.split(",")) {
                    String[] chunks = s.split(":");
                    int articleId = Integer.parseInt(chunks[0]);
                    double score = Double.parseDouble(chunks[1]);
                    if (map.containsKey(articleId)) {
                        double oldScore = map.get(articleId);
                        map.put(articleId, Math.max(oldScore, score));
                    } else
                        map.put(articleId, score);
                }
            }
            List<Pair> list = Lists.newArrayList();
            for (int articleId : map.keySet())
                list.add(new Pair(articleId, map.get(articleId)));
            return getSubSet(list, howMany);
        } catch (Exception e) {
            return EMPTY_STRING;
        }
    }
    
    public static String listToString(List<Integer> list) {
        StringBuilder sb = new StringBuilder();
        for (int element: list) 
            sb.append(element).append(",");
        deleteLastChar(sb);
        return sb.toString();
        
    }
    
    public long memoryUsage() {
        long usableFreeMemory = Runtime.getRuntime().maxMemory()
                - Runtime.getRuntime().totalMemory()
                + Runtime.getRuntime().freeMemory();
        return usableFreeMemory;
    }
    
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1,2,3,4,5);
        System.out.println(listToString(list));
        List<Pair> set = Lists.newArrayList();
        set.add(new Pair(1, 100));
        set.add(new Pair(2, 3));
        set.add(new Pair(3, 1000));
        Collections.sort(set);
        for (Pair i: set)
            System.out.println(i.getId() + " " + i.getScore());
        System.out.println(getSubSet(set, 1));
        System.out.println(getSubSet(set, 2));
        System.out.println(getSubSet(set, 3));
    }

}
